//
//  CarPayToolBar.m
//  Shengzhuli
//
//  Created by 卡忙科技 on 16/8/2.
//  Copyright © 2016年 kamangkeji. All rights reserved.
//

#import "CarPayToolBar.h"
#import "CheckButton.h"

@interface CarPayToolBar()

@property (nonatomic,weak) UIView *seprateLine;
@property (nonatomic,weak) UILabel *allLab;
@property (nonatomic,weak) UILabel *amountLab;
@property (nonatomic,weak) UIButton *payBtn;
@property (nonatomic,strong) CheckButton *checkBtn;

@end
@implementation CarPayToolBar

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        UIView *seprateLine = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenW, 0.2)];
        seprateLine.backgroundColor = [UIColor lightGrayColor];
        seprateLine.alpha = 0.6;
        self.seprateLine = seprateLine;
        [self addSubview:seprateLine];
        
        CheckButton *checkBtn = [[CheckButton alloc]initWithFrame:CGRectMake(10, (frame.size.height-25)/2.0, 80, 25)];
        [checkBtn setImage:GetImage(@"icon_select_off") forState:UIControlStateNormal];
        [checkBtn setImage:GetImage(@"icon_select_on") forState:UIControlStateSelected];
        [checkBtn setTitle:@"  全选" forState:UIControlStateNormal];
        [checkBtn setTitleColor:[UIColor darkTextColor] forState:UIControlStateNormal];
        checkBtn.titleLabel.font = Font(15);
        [checkBtn addTarget:self action:@selector(checkBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [checkBtn sizeToFit];
        self.checkBtn = checkBtn;
        [self addSubview:checkBtn];
        
        UILabel *allLab = [[UILabel alloc]init];
        allLab.font = Font(15);
        allLab.textColor = [UIColor darkTextColor];
        allLab.text = @"合计:";
        [allLab sizeToFit];
        allLab.centerY = frame.size.height*0.5;
        allLab.x = CGRectGetMaxX(checkBtn.frame)+20;
        self.allLab = allLab;
        [self addSubview:allLab];
        
        UILabel *amountLab = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(allLab.frame), allLab.y, 100, allLab.height)];
        amountLab.font = Font(16);
        amountLab.textColor = [UIColor redColor];
        amountLab.text = @"¥0.0";
        self.amountLab = amountLab;
        [self addSubview:amountLab];
        
        UIButton *payBtn = [[UIButton alloc]initWithFrame:CGRectMake(ScreenW-0.3*ScreenW, 0, 0.3*ScreenW, frame.size.height)];
        payBtn.backgroundColor = [UIColor orangeColor];
        [payBtn setTitle:@"结算" forState:UIControlStateNormal];
        [payBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        payBtn.titleLabel.font = Font(16);
        [payBtn addTarget:self action:@selector(payClick) forControlEvents:UIControlEventTouchUpInside];
        self.payBtn = payBtn;
        [self addSubview:payBtn];
    }
    return self;
}

- (void)checkBtnClick:(UIButton *)btn
{
    btn.selected = !btn.selected;
    if (![self.delegate respondsToSelector:@selector(carPayToolBarClick:select:)]) return;
    [self.delegate carPayToolBarClick:ToolBarTypeSelectAll select:btn.selected];
}
- (void)payClick
{
    if (![self.delegate respondsToSelector:@selector(carPayToolBarClick:select:)]) return;
    ToolBarType type = self.isEditing?ToolBarTypeDelete:ToolBarTypePay;
    [self.delegate carPayToolBarClick:type select:NO];
}
- (void)setIsEditing:(BOOL)isEditing
{
    _isEditing = isEditing;
    self.allLab.hidden = isEditing;
    self.amountLab.hidden = isEditing;
    if (isEditing) {
        self.payBtn.backgroundColor = [UIColor redColor];
        [self.payBtn setTitle:@"删除" forState:UIControlStateNormal];
    }else{
        self.payBtn.backgroundColor = [UIColor orangeColor];
        [self.payBtn setTitle:@"结算" forState:UIControlStateNormal];
    }
}
- (void)setIsSelected:(BOOL)isSelected
{
    _isSelected = isSelected;
    self.checkBtn.selected = isSelected;
    if (!isSelected) {
        self.amountLab.text = @"0";
    }
}
- (void)setRealMoney:(float)realMoney
{
    _realMoney = realMoney;
    self.amountLab.text = [NSString stringWithFormat:@"¥%.2f",realMoney];
}
- (void)setGoods:(NSArray *)goods
{
    _goods = goods;
    self.checkBtn.userInteractionEnabled = (goods.count !=0);
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    CGSize size = self.bounds.size;
    self.seprateLine.frame = CGRectMake(0, 0, ScreenW, 0.3);
    self.checkBtn.frame = CGRectMake(10, (size.height-25)/2.0, 60, 25);
    [self.checkBtn sizeToFit];
    self.allLab.centerY = size.height*0.5;
    self.allLab.x = CGRectGetMaxX(self.checkBtn.frame)+20;
    self.amountLab.frame = CGRectMake(CGRectGetMaxX(self.allLab.frame), self.allLab.y, 100, self.allLab.height);
    self.payBtn.frame = CGRectMake(ScreenW-0.3*ScreenW, 0, 0.3*ScreenW,size.height);
}

@end
